package com.yssoft.service.sales;

import com.yssoft.entity.core.TArea;
import com.yssoft.entity.core.TProductImg;
import com.yssoft.entity.core.TProducts;
import com.yssoft.entity.inventory.TWarehouse;
import com.yssoft.entity.sales.TSalesOrder;
import com.yssoft.entity.sales.TSalesOrderDetail;
import com.yssoft.mapper.core.TAreaMapper;
import com.yssoft.mapper.core.TPersonnelMapper;
import com.yssoft.mapper.core.TProductClassifyMapper;
import com.yssoft.mapper.core.TProductImgMapper;
import com.yssoft.mapper.core.TProductsMapper;
import com.yssoft.mapper.inventory.TWarehouseMapper;
import com.yssoft.mapper.sales.TCustomMapper;
import com.yssoft.mapper.sales.TSalesOrderDetailMapper;
import com.yssoft.mapper.sales.TSalesOrderMapper;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名称: SaleOrderService
 * @类描述: 销售订单service
 * @作者: 王永宁
 * @创建时间:2018年5月19日 下午2:24:58
 */
@Service
public class SaleOrderService {

    @Resource
    private TSalesOrderMapper saleOrderMapper;

    @Resource
    private TSalesOrderDetailMapper saleOrderDetailMapper;

    @Resource
    private TProductImgMapper productImgMapper;

    @Resource
    private TProductsMapper productMapper;

    @Resource
    private TCustomMapper customerMapper;

    @Resource
    private TAreaMapper areaMapper;

    @Resource
    private TProductClassifyMapper productClassifyMapper;


    public int querySalesOrderCount(Integer enterpriseId) {
        return saleOrderMapper.querySalesOrderCount(enterpriseId);
    }

    /**
     * 获取销售订单列表
     *
     * @param companyId
     * @param name
     * @param tcId
     * @return
     * @author 王永宁
     */
    public List<Map<String, Object>> findAll(int page, int limit, Integer companyId, String createTime, String stat) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("stat", stat);
        param.put("companyId", companyId);
        param.put("createTime", createTime);
        int start = ((page - 1) * limit);
        int end = limit;
        param.put("start", start);
        param.put("end", end);
        List<Map<String, Object>> list = saleOrderMapper.selectSaleOrderList(param);
        for (Map<String, Object> map : list) {
            List<Map<String, Object>> saleOrderDetailList = saleOrderDetailMapper.querySaleOrderDetailByOrderId(Integer.valueOf(map.get("id").toString()));
            for (Map<String, Object> map2 : saleOrderDetailList) {
                if (null != map2.get("pId")) {
                    List<Map<String, String>> imgList = productImgMapper.selectImgForSaleOrder((Integer) map2.get("pId"));
                    if (imgList.size() > 0) {
                        map2.put("img", imgList.get(0).get("img"));
                        map2.put("Timg", imgList.get(0).get("Timg"));
                    }
                }
            }
            map.put("saleOrderDetailList", saleOrderDetailList);
        }
        return list;
    }

    /**
     * @param id 销售订单id
     * @方法名称: deleteSaleOrder
     * @功能描述: 删除销售订单
     * @作者: 王永宁
     * @创建时间:2018年5月21日 上午9:11:38
     */
    public void deleteSaleOrder(Integer id) {
        TSalesOrder saleOrder = saleOrderMapper.selectByPrimaryKey(id);
        saleOrder.setDeleteFlag("1");
        saleOrderMapper.updateByPrimaryKeySelective(saleOrder);

    }

    /**
     * @param companyId 企业id
     * @param type      客户类型 1-供销商，2-客户
     * @param name      客户名称
     * @return Object
     * @方法名称: getCustomerListByEId
     * @功能描述: 根据企业id查询客户列表
     * @作者: 王永宁
     * @创建时间:2018年5月21日 上午10:07:38
     */
    public List<Map<String, Object>> getCustomerListByEId(int page, int limit, Integer companyId, String type, String name) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("type", type);
        param.put("companyId", companyId);
        param.put("name", name);
        int start = ((page - 1) * limit);
        int end = limit;
        param.put("start", start);
        param.put("end", end);
        List<Map<String, Object>> customer = customerMapper.getCustomerListByEId(param);
//		for (Map<String, Object> map : customer) {
//			Integer customerId=(Integer)map.get("id");
//			List<Map<String, Object>>	areaList=customerMapper.selectAreaByCustomerId(customerId);
//			map.put("address", areaList);
//		}
        return customer;
    }

    /**
     * @param companyId 企业id
     * @return Object
     * @方法名称: getProductMenu
     * @功能描述: 获取商品类型目录
     * @作者: 王永宁
     * @创建时间:2018年5月21日 下午3:12:38
     */
    public List<Map<String, Object>> getProductMenu(Integer companyId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("companyId", companyId);
        return productClassifyMapper.getProductMenu(param);
    }

    /**
     * @param pId       商品父类id
     * @param companyId
     * @return Object
     * @方法名称: getProductMenu
     * @功能描述: 获取商品类型目录
     * @作者: 王永宁
     * @创建时间:2018年5月21日 下午3:12:38
     */
    public List<Map<String, Object>> getProductMenu1(Integer pId, Integer companyId) {
        HashMap<String, Object> param = new HashMap<>();
        if (null != pId) {
            param.put("pId", pId);
        } else {
            param.put("pId", null);
        }
        param.put("companyId", companyId);
        return productClassifyMapper.getProductMenu1(param);
    }

    /**
     * @param id 商品类型id
     * @方法名称: queryProductDetail
     * @功能描述: 获取商品-销售订单用
     * @作者: 王永宁
     * @创建时间:2018年5月21日 上午9:09:38
     */
    public List<Map<String, Object>> queryProductDetail(Integer id, int page, int limit, String name, Integer companyId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("name", name);
        int start = ((page - 1) * limit);
        int end = limit;
        param.put("start", start);
        param.put("end", end);
        param.put("pId", id);
        param.put("companyId", companyId);
        List<Map<String, Object>> proList = productMapper.queryProductForSaleOrder(param);
        for (Map<String, Object> map : proList) {
            if (null != map.get("id")) {
                List<Map<String, String>> imgList = productImgMapper.selectImgForSaleOrder((Integer) map.get("id"));
                if (imgList.size() > 0) {
                    map.put("img", imgList.get(0).get("img"));
                    map.put("Timg", imgList.get(0).get("Timg"));
                }
            }
        }

        return proList;
    }


    /**
     * @return Object
     * @方法名称: saveSaleOrder
     * @功能描述: 保存销售订单
     * @作者: 王永宁
     * @创建时间:2018年5月22日 上午10:54:38
     */
    @Transactional
    public void saveSaleOrder(TSalesOrder order, String ids, String moneys, String amounts, String alls) {
        String code = "DD" + Calendar.getInstance().getTimeInMillis();
        if (null != order.getId()) {
            saleOrderMapper.updateByPrimaryKeySelective(order);
            TSalesOrder or = saleOrderMapper.selectByPrimaryKey(order);
            Integer orderId = order.getId();
            List<Map<String, Object>> saleOrderDetailList = saleOrderDetailMapper.querySaleOrderDetailByOrderId(orderId);
            for (Map<String, Object> map : saleOrderDetailList) {
                TSalesOrderDetail detail = new TSalesOrderDetail();
                detail.setId(Integer.valueOf(map.get("id").toString()));
                detail.setDeleteFlag("1");
                //saleOrderDetailMapper.updateByPrimaryKeySelective(detail);
                saleOrderDetailMapper.deleteByPrimaryKey(map.get("id"));
            }
            String[] idsN = ids.split(",");
            String[] moneysN = moneys.split(",");
            String[] amountsN = amounts.split(",");
            String[] allsN = alls.split(",");
            for (int i = 0; i < allsN.length; i++) {
                TSalesOrderDetail orderDetail = new TSalesOrderDetail();
                orderDetail.setCode(or.getCode());
                orderDetail.setUnitPrice(new BigDecimal(moneysN[i]));
                orderDetail.setQuantity(new BigDecimal(amountsN[i]));
                orderDetail.setSubtotalAmount(new BigDecimal(allsN[i]));
                orderDetail.setCreatorId(order.getCreatorId());
                orderDetail.setCreatorName(order.getCreatorName());
                orderDetail.setCreateTime(new Date());
                orderDetail.setDeptId(order.getDeptId());
                orderDetail.setDeptName(order.getDeptName());
                orderDetail.settEId(order.gettEId());
                orderDetail.setDeleteFlag("0");
                orderDetail.settSalesOrderId(orderId);
                orderDetail.settPId(Integer.parseInt(idsN[i]));
                saleOrderDetailMapper.insertSelective(orderDetail);
            }
        } else {
            order.setCreateTime(new Date());
            order.setCode(code);
            saleOrderMapper.insertSelective(order);
            Integer orderId = order.getId();
            String[] idsN = ids.split(",");
            String[] moneysN = moneys.split(",");
            String[] amountsN = amounts.split(",");
            String[] allsN = alls.split(",");
            for (int i = 0; i < allsN.length; i++) {
                TSalesOrderDetail orderDetail = new TSalesOrderDetail();
                orderDetail.setCode(code);
                orderDetail.setUnitPrice(new BigDecimal(moneysN[i]));
                orderDetail.setQuantity(new BigDecimal(amountsN[i]));
                orderDetail.setSubtotalAmount(new BigDecimal(allsN[i]));
                orderDetail.setCreatorId(order.getCreatorId());
                orderDetail.setCreatorName(order.getCreatorName());
                orderDetail.setCreateTime(order.getCreateTime());
                orderDetail.setDeptId(order.getDeptId());
                orderDetail.setDeptName(order.getDeptName());
                orderDetail.settEId(order.gettEId());
                orderDetail.setDeleteFlag("0");
                orderDetail.settSalesOrderId(orderId);
                orderDetail.settPId(Integer.parseInt(idsN[i]));
                saleOrderDetailMapper.insertSelective(orderDetail);
            }
        }

    }


    /**
     * @param id 销售订单id
     * @return Object
     * @方法名称: getOne
     * @功能描述: 根据销售订单获取一条记录
     * @作者: 王永宁
     * @创建时间:2018年5月22日 下午1:12:38
     */
    public Map<String, Object> getOne(Integer id) {
        Map<String, Object> order = saleOrderMapper.getOne(id);
        List<Map<String, Object>> saleOrderDetailList = saleOrderDetailMapper.querySaleOrderDetailByOrderId(id);
        order.put("saleOrderDetailList", saleOrderDetailList);
        return order;
    }


}


